package com.jhhc.utils;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;




/**
 * <p>Description:POIUtil 工具类</p>
 * <p>Copyright: Copyright (c)2019</p>
 * <p>Company: Tope</p>
 * <P>@version 1.0</P>
 */

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.xml.ws.Response;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;



@Service
public class GetDocPdfContentUtil {

//    public  String readDocText(String filePath) throws Exception {
//        File file = new File(filePath);
//        PDDocument doc = PDDocument.load(file);
//        PDFTextStripper textStripper = new PDFTextStripper();
//        String str = "";
//        for (int i = 1; i <= doc.getNumberOfPages(); i++) {
//            textStripper.setStartPage(i);
//            textStripper.setEndPage(i);
//            String s = textStripper.getText(doc);
//            System.out.println(s);
//            str = s.replaceAll("\r\n", ";");
//        }    doc.close();
//        return str;
//    }
//
//    public static String readWord(String filePath) throws Exception{
//
//
//        String buffer = "";
//        try {
//                OPCPackage opcPackage = POIXMLDocument.openPackage(filePath);
//                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
//                buffer = extractor.getText();
//            return buffer;
//        } catch (Exception e) {
//            System.out.print("error---->"+filePath);
//            e.printStackTrace();
//            return null;
//        }
//    }
//
//
//    public  String readPdfText(String path){
//        PdfDocument pdf = new PdfDocument();
//        pdf.loadFromFile(path);
//        StringBuffer sb = new StringBuffer();
//        List list = new ArrayList();
//        PdfPageBase page;
//        try{
//            for(int i = 0; i < pdf.getPages().getCount(); i ++){
//                page = pdf.getPages().get(i);
//                sb.append(page.extractText(true));
//                //.replaceAll("@ ", "@")
//                list.add(page.extractText(true).replaceAll("(.)\\1+","$1").replaceAll(" º", "º"));
//            }
//        }catch(Exception ignored){
//
//        }finally{
//            pdf.close();
//        }
//        return list.toString();
//    }

    /**
     * @Description :  文件与base64的互相转换操作
     * @Author : mabo
     */
    // filePath 为文件所在全路径: D://file.txt
    public static String fileToBase64(String filePath) {

        //定义输出流对象
        InputStream in = null;
        byte[] data = null;
        // 读取文件字节数组
        try {
            in = new FileInputStream(filePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert in != null;
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回 Base64 编码过的字节数组字符串
        assert data != null;
        return encoder.encode(data);
    }

    public static boolean str2File(String base64FileStr, String filePath) {

        // 数据为空
        if (base64FileStr == null) {
            return false;
        }
        //判断文件夹是否存在
        File outputFile = new File(filePath);
        File fileP = outputFile.getParentFile();
        if (!fileP.exists() && !fileP.mkdirs() && !fileP.mkdir()) {
            throw new IllegalArgumentException("创建文件目录失败");
        }
        BASE64Decoder decoder = new BASE64Decoder();
        OutputStream out = null;
        try {
            // Base64解码,对字节数组字符串进行Base64解码并生成文件
            byte[] byt = decoder.decodeBuffer(base64FileStr);
            for (int i = 0, len = byt.length; i < len; ++i) {
                // 调整异常数据
                if (byt[i] < 0) {
                    byt[i] += 256;
                }
            }
            InputStream input = new ByteArrayInputStream(byt);
            // 生成指定格式的文件
            out = new FileOutputStream(filePath);
            byte[] buff = new byte[1024];
            int len;
            while ((len = input.read(buff)) != -1) {
                out.write(buff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert out != null;
                out.flush();
                out.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }

}
